package com.im.partchat.mvp.persenter;


import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.im.partchat.base.IBaseView;
import com.im.partchat.mvp.model.login.LoginModel;
import com.im.partchat.mvp.model.login.LoginModelImpl;
import com.im.partchat.utils.MD5Utils;
import com.im.partchat.utils.rxlifecycle.compact.RxLifecycleCompact;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Predicate;

/**
 * Created by flyou on 2017/6/30.
 * VersionCode: 1
 * Desc:
 */

public class ChangePasswordPersenter<T> extends BasePresenter {
    private IBaseView<String> baseView;
    private LoginModel loginModel;
    private AppCompatActivity context;

    public ChangePasswordPersenter(IBaseView<String> baseView, AppCompatActivity fragment) {
        this.baseView = baseView;
        this.context = fragment;
        this.loginModel = new LoginModelImpl();
    }

    public void getMainData(String userId, String oldPassword, String newPassword) {
        String pwd = MD5Utils.getPwd(oldPassword);
        String newPwd = MD5Utils.getPwd(newPassword);
        responseCompose(loginModel.changePaw(userId, pwd, newPwd), baseView, "修改密码失败")

                .filter(new Predicate<String>() {
                    @Override
                    public boolean test(String s) throws Exception {
                        return !TextUtils.isEmpty(s);
                    }
                })
                .compose(RxLifecycleCompact.bind(context).<String>withObservable())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        baseView.showLoading("正在修改密码");
                    }

                    @Override
                    public void onNext(String value) {

                        baseView.onLoadDataSuccess(value, "");
                    }

                    @Override
                    public void onError(Throwable e) {
                        baseView.hideLoading();
                        baseView.showError(NetErrorObservable(e));
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        baseView.hideLoading();
                    }
                });
    }


}
